package org.harryrose.resultkit.writers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.harryrose.resultkit.converters.DataConverter;
import org.harryrose.resultkit.converters.DefaultStringDataConverter;
import org.harryrose.resultkit.exceptions.CannotWriteException;
import org.harryrose.resultkit.exceptions.TypeException;

/** 
 * Writes variables to disk
 * @author harry
 */
public class CSVWriter extends ResultWriter{

	@Override
	public void write(File directory, String variableName, Object value) throws CannotWriteException, TypeException {
		write(directory,variableName,value,new DefaultStringDataConverter());
	}

	@Override
	public <T> void write(File directory, String variableName, Object value,
			DataConverter<T> converter) throws CannotWriteException, TypeException {
		try
		{
			FileWriter writer = new FileWriter(new File(directory,variableName+".csv"));
			writer.write(converter.convert(value).toString());
			writer.close();
		}
		catch(IOException e)
		{
			throw new CannotWriteException("Unable to write variable '"+variableName+"'.", e);
		}
	}
	
	@Override
	public void write(File directory, String variableName, Object[] array) throws CannotWriteException, TypeException {
		write(directory,variableName,array,new DefaultStringDataConverter());
	}

	@Override
	public <T> void write(File directory, String variableName, Object[] array,
			DataConverter<T> converter) throws CannotWriteException,
			TypeException {
		try
		{
			FileWriter writer = new FileWriter(new File(directory,variableName+".csv"));
			
			if(array.length > 0)
			{
				writer.write(array[0].toString());
			}
			for(int i = 1; i < array.length; i++)
			{
				writer.write(",");
				writer.write(array[i].toString());
			}
			writer.close();
		}
		catch(IOException e)
		{
			throw new CannotWriteException("Unable to write variable '"+variableName+"'.", e);
		}
	}

	@Override
	/**
	 * Writes a 2D array to file.  
	 * 
	 * When direction of fields are represented by the file (i.e. x and y values are in a grid), x should be written horizontally, y should be vertically.
	 * For example, consider variable[x1..x3][y1..y3]
	 * The values in the CSV will be represented as follows
	 * 
	 *  	[x1][y1],	[x2][y1],	[x3][y1]
	 *  	[x1][y2],	[x2][y2],	[x3][y2]
	 *  	[x1][y3],	[x2][y3],	[x3][y3]
	 */
	public void write(File directory, String variableName, Object[][] array) throws CannotWriteException{
		try
		{
			FileWriter writer = new FileWriter(new File(directory,variableName+".csv"));
			
			///ASSUMPTION: All sub-arrays are the same length (this is not necessarily the case!)
			for(int y = 0; y < array[0].length; y++)
			{
				if(y > 0) writer.write("\n");
				
				if(array[0].length > 0)
				{
					writer.write(array[0][y].toString());
				}
				for(int x = 1; x < array.length; x++)
				{
					writer.write(",");
					writer.write(array[x][y].toString());
				}
			}
			
			writer.close();
		}
		catch(IOException e)
		{
			throw new CannotWriteException("Unable to write variable '"+variableName+"'.", e);
		}
	}

	
	@Override
	public <T> void write(File directory, String variableName,
			Object[][] array, DataConverter<T> converter)
			throws CannotWriteException, TypeException {
		// TODO Auto-generated method stub
		
	}

	
}
